﻿@model AffiliateModel
@Html.ValidationSummary(false)
@Html.HiddenFor(model => model.Id)

<div id="affiliate-edit">
    <ul>
        <li @Html.RenderSelectedTabIndex(0, GetSelectedTabIndex())>
            @T("Admin.Affiliates.Info")
        </li>
        @if (Model.Id > 0)
        {
            <li @Html.RenderSelectedTabIndex(1, GetSelectedTabIndex())>
                @T("Admin.Affiliates.Customers")
            </li>
            <li @Html.RenderSelectedTabIndex(2, GetSelectedTabIndex())>
                @T("Admin.Affiliates.Orders")
            </li>
        }
    </ul>
    <div>
        @TabInfo()
    </div>
    @if (Model.Id > 0)
    {
        <div>
            @TabCustomers()
        </div>
        <div>
            @TabOrders()
        </div>
    }
</div>
<script>
    $(document).ready(function() {
        $("#affiliate-edit").kendoTabStrip({
            animation:  {
                open: {
                    effects: "fadeIn"
                }
            },
            select: tabstrip_on_tab_select
        });
    });
</script>

@{
    //custom tabs
    var eventMessage = new AdminTabStripCreated(this.Html, "affiliate-edit");
    EngineContext.Current.Resolve<IEventPublisher>().Publish(eventMessage);
    foreach (var eventBlock in eventMessage.BlocksToRender)
    {
        @eventBlock
    }
}

@*save selected tab index*@
<input type="hidden" id="selected-tab-index" name="selected-tab-index" value="@(GetSelectedTabIndex())">

@helper TabInfo()
    {        
    <table class="adminContent">
        @if (Model.Id > 0)
        {
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.Id):
                </td>
                <td class="adminData">
                    @Model.Id
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.Url):
                </td>
                <td class="adminData">
                    @Model.Url
                </td>
            </tr>
        }
        <tr>
            <td class="adminTitle">
                @Html.NopLabelFor(model => model.Active):
            </td>
            <td class="adminData">
                @Html.EditorFor(model => model.Active)
                @Html.ValidationMessageFor(model => model.Active)
            </td>
        </tr>
    </table>
        
    @Html.EditorFor(model => model.Address, "Address")
}
@helper TabCustomers()
    {
    var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
    var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;


    <div id="customers-grid"></div>
        
    <script>
    $(document).ready(function () {
        $("#customers-grid").kendoGrid({
            dataSource: {
                type: "json",
                transport: {
                    read: {
                        url: "@Html.Raw(Url.Action("AffiliatedCustomerList", "Affiliate", new { affiliateId = Model.Id }))",
                        type: "POST",
                        dataType: "json"
                    }
                },
                schema: {
                    data: "Data",
                    total: "Total",
                    errors: "Errors"
                },
                error: function(e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                },
                pageSize: @(defaultGridPageSize),
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true
            },
            pageable: {
                refresh: true,
                pageSizes: [@(gridPageSizes)]
            },
            editable: {
                confirmation: false,
                mode: "inline"
            },
            scrollable: false,
            columns: [{
                field: "Id",
                title: "ID",
                width: 50
            }, {
                field: "Name",
                title: "@T("Admin.Affiliates.Customers.Name")",
                width: 200
            }, {
                field: "Id",
                title: "@T("Admin.Common.View")",
                width: 100,
                template: '<a href="@Url.Content("~/Admin/Customer/Edit/")#=Id#">@T("Admin.Common.View")</a>'
            }]
        });
    });
</script>
}
@helper TabOrders()
    {
        
        var defaultGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().DefaultGridPageSize;
        var gridPageSizes = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSizes;

    <div id="order-grid"></div>
        
    <script>
        $(document).ready(function () {
            $("#order-grid").kendoGrid({
                dataSource: {
                    type: "json",
                    transport: {
                        read: {
                            url: "@Html.Raw(Url.Action("AffiliatedOrderList", "Affiliate", new { affiliateId = Model.Id }))",
                            type: "POST",
                            dataType: "json"
                        }
                    },
                    schema: {
                        data: "Data",
                        total: "Total",
                        errors: "Errors"
                    },
                    error: function(e) {
                        display_kendoui_grid_error(e);
                        // Cancel the changes
                        this.cancelChanges();
                    },
                    pageSize: @(defaultGridPageSize),
                    serverPaging: true,
                    serverFiltering: true,
                    serverSorting: true
                },
                pageable: {
                    refresh: true,
                    pageSizes: [@(gridPageSizes)]
                },
                editable: {
                    confirmation: false,
                    mode: "inline"
                },
                scrollable: false,
                columns: [{
                    field: "Id",
                    title: "@T("Admin.Affiliates.Orders.Order")",
                    width: 50
                }, {
                    field: "OrderStatus",
                    title: "@T("Admin.Affiliates.Orders.OrderStatus")"
                }, {
                    field: "PaymentStatus",
                    title: "@T("Admin.Affiliates.Orders.PaymentStatus")"
                },{
                    field: "ShippingStatus",
                    title: "@T("Admin.Affiliates.Orders.ShippingStatus")"
                },{
                    field: "OrderTotal",
                    title: "@T("Admin.Affiliates.Orders.OrderTotal")"
                },{
                    field: "CreatedOn",
                    title: "@T("Admin.Affiliates.Orders.CreatedOn")",
                    type: "date",
                    format: "{0:G}"
                },{
                    field: "Id",
                    title: "@T("Admin.Common.View")",
                    width: 100,
                    template: '<a href="@Url.Content("~/Admin/Order/Edit/")#=Id#">@T("Admin.Common.View")</a>'
                }]
            });
        });
    </script>
}